<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/11/21
 * Time: 10:49 AM
 */

namespace Biz\module\Decoupling\refund;

use Biz\module\Classes\OrderSms as clsOrderSms;
use Biz\module\Decoupling\AbstractMain;
use Biz\service\Event;
use Doraemon\tools\Log as tLog;
use Doraemon\amqp\Queue as aQueue;
use Biz\model\trade\OrderOperateLog as mOrderOperateLog;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\model\ShopBasicInfo as mShopBasicInfo;


class ApplyRefund extends AbstractMain
{
    const NAME = '退款申请';


    public function main($message, $data)
    {
        //解析 json 并赋值变量
        $appendData = [
            'orderShops' => [],
            'uid' => $data['body']['uid'],
            'addTime' => $data['header']['addtime'],
            'orderNo' => $data['body']['order_no'],
            'skuId' => $data['body']['sku_id'],
            'refundOrderNo' => $data['body']['refund_order_no'],
            'needOrderOpLog' => $data['body']['need_order_oplog'],
            'typeRefund' => $data['body']['type_refund'],
            'isEdit' => $data['body']['is_change'],
            'receiptStatus' => $data['body']['receipt_status'],
            'reason' => $data['body']['reason'],
            'refundRemark' => $data['body']['refund_remark'],
            'imagesContent' => $data['body']['images_content'],
            'body' => $data['body'],

            //获取所有的退款原因
            'reasonList' => mOrderRefund::getAllRefundReasonList(),
        ];

        $appendData['orderSku'] = mOrderGoods::getSkuInfo($appendData['orderNo'], $appendData['skuId']);

        $this->appendData($appendData);

    }


    protected function _exec_order_operate_log()
    {
        //插入订单操作日志表
        if ($this->data['needOrderOpLog']) {

            //插入记录到order_operate_log表
            $operateType = mOrderOperateLog::OPERATE_TYPE_APPLY_REFUND_AMOUNT;
            if ($this->data['typeRefund'] == mOrderRefund::TYPE_REFUND_ALL) {
                $operateType = mOrderOperateLog::OPERATE_TYPE_APPLY_REFUND_ALL;
            }

            $result = mOrderOperateLog::createLog($this->data['orderSku']['shop_id'], $this->data['orderSku']['order_no'], mOrderOperateLog::TRADE_TYPE_ORDER_REFUND, [
                'operate_type' => $operateType,
                'operate_user_type' => mOrderOperateLog::OPERATE_USERTYPE_USER,
                'operate_uid' => $this->data['orderSku']['uid'],
            ], $this->data['refundOrderNo'], $textContent = vsprintf('%s，退款类型：%s 货物状态：%s 原因：%s 备注:%s', [
                $this->data['isEdit'] ? '重新提交申请' : '提交申请',
                mOrderRefund::$typeRefundDesc[$this->data['typeRefund']],
                !empty($this->data['receiptStatus']) ? mOrderRefund::$receiptStatusDesc[$this->data['receiptStatus']] : '未收到货',
                !empty($this->data['reason']) ? $this->data['reasonList'][$this->data['reason']] : '',
                !empty($this->data['refundRemark']) ? $this->data['refundRemark'] : '无'
            ]), $this->data['imagesContent'], $this->data['body']);


            if(is_object($result) && $result->rowCount() > 0){
                $this->appendLog('记录交易日志', array_merge($this->data, ['text_content' => $textContent]));

                return true;
            }

            return false;
        }
    }

    protected function _exec_send_msg_to_adminer()
    {
        if (!$this->data['isEdit']) {
            $shopInfo = mShopBasicInfo::getShopInfo($this->data['orderSku']['shop_id'], ['name']);
            aQueue::insertQueue("queue/Wechatnotice/toTag", $msgContent = [
                "totag" => \Biz\pockets\constInterface\BizWechatTagConfig::TAG_BIZ_BUSINESS_REFUND,
                "content" => vsprintf('［有新的退款］%s的订单：%s 客户提交了售后申请:%s,货物状态：%s 原因为：%s 备注：%s', [
                    $shopInfo['name'],
                    $this->data['orderSku']['order_no'],
                    mOrderRefund::$typeRefundDesc[$this->data['typeRefund']],
                    !empty($this->data['receiptStatus']) ? mOrderRefund::$receiptStatusDesc[$this->data['receiptStatus']] : '未收到货',
                    !empty($this->data['reason']) ? $this->data['reasonList'][$this->data['reason']] : '',
                    !empty($this->data['refundRemark']) ? $this->data['refundRemark'] : '无'
                ])
            ], aQueue::QUEUE_BIZ);

            $this->appendLog('如果是新申请则给客服发通知', array_merge($this->data, ['msg_content' => $msgContent]));

        }
    }

    protected function _exec_new_refund_order_notice()
    {

        clsOrderSms::noticeNewRefundOrder($refundInfo = ['shop_id' => $this->data['orderSku']['shop_id'], 'order_no' => $this->data['orderNo']]);

        $this->appendLog('给商家退款专员发送短信提醒', array_merge($this->data, ['refund_info' => $refundInfo]));


    }

    protected function _exec_nippon()
    {
        \Biz\service\erp\Nippon::addRefund($this->data['refundOrderNo'], $this->data['orderNo'], $this->data['orderSku']);
    }
}